package com.deman.framework.rpc.netty;

import com.deman.framework.rpc.core.DefaultInvoker;
import com.deman.framework.rpc.core.Invoker;
import com.deman.framework.rpc.model.MessageRequest;
import com.deman.framework.rpc.model.MessageResponse;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ServerHandler extends ChannelInboundHandlerAdapter {
    Logger logger = LoggerFactory.getLogger(ServerHandler.class);

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        logger.debug("{}", msg);
        if (msg instanceof MessageRequest) {


            MessageRequest messageRequest = (MessageRequest) msg;
            logger.debug("{}", messageRequest.getMessageId());
            Invoker methodCall = new DefaultInvoker();

            MessageResponse messageResponse = methodCall.invoke(messageRequest);

            ctx.channel().writeAndFlush(messageResponse).addListener(new ChannelFutureListener(){
                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                        if(future.isSuccess()){

                        }
                }
            });

        }
    }


}
